<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>项目管理 - 项目列表</title>
    <link rel="stylesheet" href="styles.css">
    <style>
        .projects-container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }
        .search-filters {
            background: #f8f9fa;
            padding: 20px;
            border-radius: 8px;
            margin-bottom: 20px;
        }
        .filter-row {
            display: flex;
            gap: 15px;
            align-items: center;
            margin-bottom: 15px;
        }
        .filter-group {
            display: flex;
            flex-direction: column;
        }
        .filter-group label {
            font-weight: bold;
            margin-bottom: 5px;
        }
        .filter-group input,
        .filter-group select {
            padding: 8px 12px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        .search-btn {
            background: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 4px;
            cursor: pointer;
            margin-top: 20px;
        }
        .projects-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
            gap: 20px;
        }
        .project-card {
            background: white;
            border: 1px solid #e0e0e0;
            border-radius: 8px;
            padding: 20px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
        }
        .project-title {
            font-size: 18px;
            font-weight: bold;
            margin-bottom: 10px;
        }
        .project-status {
            padding: 4px 8px;
            border-radius: 12px;
            font-size: 12px;
            font-weight: bold;
        }
        .status-active { background: #d4edda; color: #155724; }
        .status-completed { background: #cce5ff; color: #004085; }
        .status-paused { background: #fff3cd; color: #856404; }
        .progress-bar {
            width: 100%;
            height: 8px;
            background: #e9ecef;
            border-radius: 4px;
            margin: 10px 0;
        }
        .progress-fill {
            height: 100%;
            background: #007bff;
        }
        .btn {
            padding: 8px 16px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            text-decoration: none;
            display: inline-block;
            margin-right: 10px;
        }
        .btn-primary { background: #007bff; color: white; }
        .btn-secondary { background: #6c757d; color: white; }
    </style>
</head>
<body>
    <div class="header">
        <div class="header-content">
            <h1>项目管理系统</h1>
            <div class="user-info">
                <span id="userDisplay">欢迎，用户</span>
                <button class="logout-btn" onclick="logout()">退出登录</button>
            </div>
        </div>
    </div>

    <div class="projects-container">
        <div class="search-filters">
            <h2>项目查询</h2>
            <div class="filter-row">
                <div class="filter-group">
                    <label for="projectName">项目名称</label>
                    <input type="text" id="projectName" placeholder="输入项目名称关键词">
                </div>
                <div class="filter-group">
                    <label for="projectStatus">项目状态</label>
                    <select id="projectStatus">
                        <option value="">全部状态</option>
                        <option value="active">进行中</option>
                        <option value="completed">已完成</option>
                        <option value="paused">已暂停</option>
                    </select>
                </div>
            </div>
            <button class="search-btn" onclick="searchProjects()">搜索项目</button>
        </div>

        <div id="projectsContent">
            <div class="loading">加载中...</div>
        </div>
    </div>

    <script>
        let allProjects = [];
        let filteredProjects = [];

        document.addEventListener('DOMContentLoaded', function() {
            checkAuthAndLoadProjects();
        });

        function checkAuthAndLoadProjects() {
            const token = localStorage.getItem('authToken');
            const userInfo = localStorage.getItem('userInfo');

            if (!token || !userInfo) {
                alert('请先登录');
                window.location.href = 'login.html';
                return;
            }

            try {
                const user = JSON.parse(userInfo);
                document.getElementById('userDisplay').textContent = `欢迎，${user.username} (${getRoleName(user.role)})`;
            } catch (error) {
                console.error('解析用户信息失败:', error);
            }

            loadProjects();
        }

        function getRoleName(role) {
            const roleNames = {
                'minister': '部长',
                'pm': '项目负责人',
                'frontend': '前端负责人',
                'backend': '后端负责人',
                'member': '普通成员'
            };
            return roleNames[role] || role;
        }

        async function loadProjects() {
            const token = localStorage.getItem('authToken');
            const projectsContent = document.getElementById('projectsContent');

            try {
                projectsContent.innerHTML = '<div class="loading">加载中...</div>';

                const response = await fetch('/projects', {
                    method: 'GET',
                    headers: {
                        'Authorization': `Bearer ${token}`
                    }
                });

                const data = await response.json();

                if (data.code === 200) {
                    allProjects = data.data;
                    filteredProjects = [...allProjects];
                    displayProjects(filteredProjects);
                } else {
                    projectsContent.innerHTML = `<div class="error">获取项目列表失败: ${data.message}</div>`;
                }
            } catch (error) {
                console.error('加载项目失败:', error);
                projectsContent.innerHTML = '<div class="error">网络错误，请重试</div>';
            }
        }

        function searchProjects() {
            const projectName = document.getElementById('projectName').value.toLowerCase();
            const projectStatus = document.getElementById('projectStatus').value;

            filteredProjects = allProjects.filter(project => {
                const nameMatch = !projectName || project.name.toLowerCase().includes(projectName) || 
                                project.description.toLowerCase().includes(projectName);
                const statusMatch = !projectStatus || project.status === projectStatus;
                
                return nameMatch && statusMatch;
            });

            displayProjects(filteredProjects);
        }

        function displayProjects(projects) {
            const projectsContent = document.getElementById('projectsContent');

            if (projects.length === 0) {
                projectsContent.innerHTML = '<div class="no-projects">没有找到符合条件的项目</div>';
                return;
            }

            const projectsHtml = `
                <div class="projects-grid">
                    ${projects.map(project => `
                        <div class="project-card">
                            <div class="project-header">
                                <h3 class="project-title">${project.name}</h3>
                                <span class="project-status status-${project.status}">${getStatusText(project.status)}</span>
                            </div>
                            <p>${project.description}</p>
                            <div class="progress-bar">
                                <div class="progress-fill" style="width: ${project.progress}%"></div>
                            </div>
                            <p>进度: ${project.progress}% | 负责人: ${project.pm_name}</p>
                            <p>时间: ${project.start_date} - ${project.end_date}</p>
                            <div class="project-actions">
                                <a href="project-detail.html?id=${project.id}" class="btn btn-primary">查看详情</a>
                                <a href="${project.git_url}" target="_blank" class="btn btn-secondary">Git仓库</a>
                            </div>
                        </div>
                    `).join('')}
                </div>
            `;

            projectsContent.innerHTML = projectsHtml;
        }

        function getStatusText(status) {
            const statusTexts = {
                'active': '进行中',
                'completed': '已完成',
                'paused': '已暂停'
            };
            return statusTexts[status] || status;
        }

        function logout() {
            localStorage.removeItem('authToken');
            localStorage.removeItem('userInfo');
            window.location.href = 'login.html';
        }
    </script>
</body>
</html> 